package cn.topcode.unicorn.wxsdk.menu;

import java.util.HashMap;
import java.util.Map;

import cn.topcode.unicorn.utils.http.HttpUtil;
import cn.topcode.unicorn.wxsdk.WXContext;
import cn.topcode.unicorn.wxsdk.base.ApiUrl;
import cn.topcode.unicorn.wxsdk.base.Result;
import cn.topcode.unicorn.wxsdk.menu.dto.CreateMenu;
import cn.topcode.unicorn.wxsdk.menu.dto.CreatePersonalMenu;
import cn.topcode.unicorn.wxsdk.menu.dto.CreatePersonalMenuResult;
import cn.topcode.unicorn.wxsdk.menu.dto.QueryMenuResult;
import cn.topcode.unicorn.wxsdk.menu.dto.TestPersonalMenuResult;

public class MenuInvokerImpl implements MenuInvoker {
    
    @Override
    public Result createMenu(CreateMenu menu) {
        String token = WXContext.getToken();
        String url = String.format(ApiUrl.Menu.CREATE_MENU, token);
        return HttpUtil.post(menu, Result.class, url);
    }

    @Override
    public QueryMenuResult queryMenuResult() {
        String token = WXContext.getToken();
        String url = String.format(ApiUrl.Menu.QUERY_MENU, token);
        return HttpUtil.getJson(url, QueryMenuResult.class);
    }

    @Override
    public Result deleteMenu() {
        String token = WXContext.getToken();
        String url = String.format(ApiUrl.Menu.DELETE_MENU, token);
        return HttpUtil.getJson(url, Result.class);
    }

    @Override
    public CreatePersonalMenuResult createPersonalMenu(CreatePersonalMenu menu) {
        String token = WXContext.getToken();
        String url = String.format(ApiUrl.Menu.CREATE_PERSONAL_MENU, token);
        return HttpUtil.post(menu, CreatePersonalMenuResult.class, url);
    }

    @Override
    public Result deletePersonalMenu(String menuId) {
        String token = WXContext.getToken();
        String url = String.format(ApiUrl.Menu.DELETE_PERSONAL_MENU, token);
        Map<String,Object> map = new HashMap<>();
        map.put("menuid", menuId);
        return HttpUtil.post(map, Result.class, url);
    }

    @Override
    public TestPersonalMenuResult testPersonalMenuResult(String userId) {
        String token = WXContext.getToken();
        String url = String.format(ApiUrl.Menu.TEST_PERSONAL_MENU, token);
        Map<String,Object> map = new HashMap<>();
        map.put("user_id", userId);
        return HttpUtil.post(map, TestPersonalMenuResult.class, url);
    }

}
